//
//  SN_ResoQuestion_TableViewCell.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/21.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_ResoQuestion_TableViewCell.h"

#import "SN_Question_Solve_Model.h"

@implementation SN_ResoQuestion_TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
    }
    
    return self;
}


- (void)setModel:(SN_Question_SolveList_Model *)model {
    
    _model = model;
    
    NSInteger count = model.questionlist.count;
    
    for (UIView *subView in self.contentView.subviews) {
        
        [subView removeFromSuperview];
    }
    
    CGFloat x,y,w,h;
    w = 56;  //宽度
    int num  = 5;  //一行数量
    CGFloat leftMargin = 15;
    
    CGFloat spaceX = (SCREENWIDTH - (leftMargin + leftMargin) - num * w) / num - 1;
    CGFloat spaceY = 8;
    h = 26;
    
    for (int i = 0; i < count; i++) {
        
        x = leftMargin + (i % num) * (w + spaceX);
        y = 0 + i / num * (h + spaceY);
        _questionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _questionButton.frame = CGRectMake(x, y, w, h);
        
        _questionButton.layer.borderWidth = 0.5;
        _questionButton.layer.borderColor = [UIColor colorWithHexString:@"#dedede"].CGColor;
        _questionButton.layer.cornerRadius = 5;
        [_questionButton setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
        _questionButton.tag = i;
        [_questionButton addTarget:self action:@selector(questionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _questionButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [_questionButton setTitleColor:[UIColor colorWithHexString:@"#cccccc"] forState:UIControlStateSelected];
        
        SN_Question_Solve_Model *questionModel = [model.questionlist objectAtIndex:i];
        
        _questionButton.selected = questionModel.isread;
        [_questionButton setTitle:[NSString stringWithFormat:@"%d题",i+1] forState:UIControlStateNormal];
//        [_questionButton setBackgroundColor:[UIColor colorWithHexString:@"#"]]
        [_questionButton setBackgroundColor:[UIColor whiteColor]];
        
        [self.contentView addSubview:_questionButton];
    }
    
    
}

- (void)questionBtnClick:(UIButton *)sender {
    
    sender.selected = YES;
    
    if (_delegate && [_delegate respondsToSelector:@selector(clickCellButtonWithModel:index: allModel:)]) {
        
        [_delegate clickCellButtonWithModel:self.model index:sender.tag allModel:self.dataArr];
    }
}

@end
